#include <pthread.h>
#include <stdio.h>
#include <strings.h>

#define THREAD_NUM 5
#define ARRAY_NUM 5

void* print_xs (void* par)
{
	pthread_t tid = pthread_self();
	printf ("The thread %lu is here\n", tid);

	char p[1024*1024*1];
	int i2=1024*1024*1;

	while(i2--)
	{
		p[i2] = 3;
	}
	printf("p is %08p\n", p);
	pthread_exit("exit\n");
	//return NULL;
}

int main()
{

	pthread_t thread[THREAD_NUM];
	pthread_attr_t thread_attr;
	size_t stack_size;
	int status;

	status = pthread_attr_init (&thread_attr);
	if (status != 0)
		printf ("Create attr, %d", status);

#ifdef _POSIX_THREAD_ATTR_STACKSIZE
	status = pthread_attr_getstacksize (&thread_attr, &stack_size);
	if (status != 0)
		printf ("Get stack size, %d", status);
	printf ("Default stack size is %u; minimum is %u\n",stack_size, PTHREAD_STACK_MIN);

	status = pthread_attr_setstacksize (&thread_attr, PTHREAD_STACK_MIN*1024);
	if (status != 0)
		printf ("Set stack size, %d", status);

	status = pthread_attr_getstacksize (&thread_attr, &stack_size);
	if (status != 0)
		printf ("Get stack size, %d", status);
	printf ("Default stack size is %u; minimum is %u\n",stack_size, PTHREAD_STACK_MIN);
#endif

	printf ("Main starting\n");

	printf ("========================\n");
	int p[ARRAY_NUM];
	int i2 = 0;
	printf("i2 is %08p\n", &i2);

	for( i2 = 0; i2 < ARRAY_NUM; i2++ )
	{
		p[i2] = 3;
		printf("p[%d] is %d\n", i2,p[i2]);
	}
	printf ("========================\n");

	printf ("========================\n");
	int i3 = 0;
	for( i3 = 0; i3 < THREAD_NUM; i3++ )
	{
		status = pthread_create(&thread[i3], &thread_attr, &print_xs, NULL);
		if (status != 0){
			printf ("Create thread failed!!\n");
		}
		else{
			//main thread will stop here util all thread are finished... 
			pthread_join(thread[i3],NULL);
		}
	}

	printf ("Total thread is %d\n", i3);

	printf ("Main exiting\n");
	printf ("========================\n");

	pthread_exit("exit\n");
	//return 0;
}
